package com.hjm.project.agentInfo.account.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.DesUtil;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.framework.config.CustomConfig;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.clientImg.clientImg.domain.ClientImg;
import com.hjm.project.clientImg.clientImg.mapper.ClientImgMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.agentInfo.account.mapper.AccountMapper;
import com.hjm.project.agentInfo.account.domain.Account;
import com.hjm.project.agentInfo.account.service.IAccountService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机构账户Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-27
 */
@Service
public class AccountServiceImpl implements IAccountService 
{
    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private ClientImgMapper clientImgMapper;

    /**
     * 查询机构账户
     * 
     * @param id 机构账户ID
     * @return 机构账户
     */
    @Override
    public Account selectAccountById(Long id)
    {
        return accountMapper.selectAccountById(id);
    }

    /**
     * 查询机构账户列表
     * 
     * @param account 机构账户
     * @return 机构账户
     */
    @Override
    public List<Account> selectAccountList(Account account)
    {
        return accountMapper.selectAccountList(account);
    }

    /**
     * 新增机构账户
     * 
     * @param account 机构账户
     * @return 结果
     */
    @Override
    public int insertAccount(Account account)
    {
        return accountMapper.insertAccount(account);
    }

    /**
     * 修改机构账户
     * 
     * @param account 机构账户
     * @return 结果
     */
    @Override
    public int updateAccount(Account account)
    {
        return accountMapper.updateAccount(account);
    }

    /**
     * 删除机构账户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAccountByIds(String ids)
    {
        return accountMapper.deleteAccountByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机构账户信息
     * 
     * @param id 机构账户ID
     * @return 结果
     */
    @Override
    public int deleteAccountById(Long id)
    {
        return accountMapper.deleteAccountById(id);
    }

    /**
     * 虚拟账户垫资充值
     * @param file MultipartFile
     * @param account Account
     * @return int
     * type //请求类型 1 充值附带扣款 2、垫资充值 总后台确认默认为 1
     */
    @Override
    public AjaxResult saveBalance(MultipartFile file, Account account,String type) {

        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            return AjaxResult.error("请上传凭证");
        }

        int result = 0;
        //校验密码
        String password = account.getPassword();
        if (StringUtils.isEmpty(password) || !"963852".equals(password)){
            return AjaxResult.error("密码输入错误");
        }

        //请求类型 1 充值附带扣款 2、垫资充值 总后台确认默认为 1
        Map<String, Object> paramsOne = Maps.newHashMap();
        paramsOne.put("agent_num", account.getAgentNum());
        paramsOne.put("agent_name", account.getAgentName());
        paramsOne.put("money", account.getAccountMoney());
        paramsOne.put("note", account.getNote());
        paramsOne.put("type",type);
        //操作员
        paramsOne.put("man", account.getCreateMan());
        //垫支名称 默认写死
        paramsOne.put("underlay_name", "公司总账户");
        //垫支编号  默认写死
        paramsOne.put("underlay_num", "000000");
        String md5Hex = DigestUtils.md5Hex(JSON.toJSONString(paramsOne).getBytes());
        JSONObject obj = new JSONObject();
        obj.put("body", paramsOne);
        obj.put("md5", md5Hex);
        try {
            String encrypt = DesUtil.encrypt(obj.toString().getBytes(), DesUtil.KEY);
            //TODO 服务商管理-服务商账户管理-代理账户显示 请求虚拟账户
            String s = DesUtil.sendData(CustomConfig.getVirtualAccountUrl(),CustomConfig.getVirtualAccountPort(),encrypt.getBytes());
            if (StringUtils.isNotEmpty(s)){
                JSONObject jsonObject = JSON.parseObject(s);
                String code = String.valueOf(jsonObject.get("code"));
                String message = String.valueOf(jsonObject.get("message"));
                String body = String.valueOf(jsonObject.get("body"));
                if("1000".equals(code)){
                    //充值成功
                    //上传图片
                    String fileName =  file.getOriginalFilename();
                    Map<String, String> metaList = Maps.newHashMap();
                    metaList.put("fileName",fileName);
                    metaList.put("fileType", fileName.substring(fileName.lastIndexOf(".")+1));
                    metaList.put("author", account.getAgentName());
                    metaList.put("date", DateUtils.getDateTime());
                    File copyFile = File.createTempFile("prefix", "_" + file.getOriginalFilename());
                    file.transferTo(copyFile);
                    String fid = FastDFSClient.uploadFile(copyFile, copyFile.getName(), metaList);
                    String fastGroup = fid.substring(0, 17);
                    ClientImg clientImg = new ClientImg();
                    clientImg.setClientno(account.getAgentNum());
                    clientImg.setFileid(fid);
                    clientImg.setFastdfsGroup(fastGroup);
                    clientImg.setCreatename(account.getAgentName());
                    clientImg.setCreatetime(new Date());
                    clientImg.setType(Long.parseLong("34"));
                    clientImg.setObjType(Long.parseLong("1"));
                    clientImg.setClientSerial(body);
                    clientImgMapper.insertclientImg(clientImg);
                } else {
                    return AjaxResult.error(message);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("操作异常:" + e.getMessage());
        }
        return AjaxResult.success();
    }
}
